// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	"name": {
		"rules": [{
				"required": true,
				"errorMessage": '请填写标题',
			},
			{
				"minLength": 1,
				"maxLength": 23,
				"errorMessage": '{label}长度在 {minLength} 到 {maxLength} 个字符',
			},
			{
				"format": "string"
			}
		],
		"label": "标题"
	},
	"content": {
		"rules": [{
			"required": true,
			"errorMessage": '请填写详情',
		}, {
			"format": "string"
		}],
		"label": "详情"
	},
	"imgs": {
		"rules": [{
				"required": true,
				"errorMessage": '请选择活动图片',
			}, {
				"format": "array"
			},
			{
				"maxLength": 6
			}
		],
		"label": "图片"
	},
	"tags": {
		"rules": [{
				"format": "array"
			},
			{
				"arrayType": "string"
			},
			{
				"maxLength": 6
			}
		],
		"label": "标签"
	},
	"type": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择活动分类',
		}, {
			"format": "string"
		}],
		"label": "分类"
	},
	"signup_start_time": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择报名开始时间',
		}, {
			"format": "timestamp"
		}],
		"label": "报名开始时间"
	},
	"signup_end_time": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择报名结束时间',
		}, {
			"format": "timestamp"
		}],
		"label": "报名结束时间"
	},
	"start_time": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择活动开始时间',
		}, {
			"format": "timestamp"
		}],
		"label": "活动开始时间"
	},
	"end_time": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择活动结束时间',
		}, {
			"format": "timestamp"
		}],
		"label": "活动结束时间"
	},
	"location": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择活动地点',
		}, {
			"format": "string"
		}],
		"label": "活动地点"
	},
	"location_lat": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择纬度',
		}, {
			"format": "string"
		}],
		"label": "地点纬度"
	},
	"location_lng": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择经度',
		}, {
			"format": "string"
		}],
		"label": "地点经度"
	},
	"is_pay": {
		"rules": [{
			"format": "bool"
		}],
		"label": "是否付费"
	},
	"pay_amount": {
		"rules": [{
			"format": "int"
		}],
		"label": "付费金额"
	},
	"people_num": {
		"rules": [{
			"required": true,
			"errorMessage": '请选择活动人数',
		}, {
			"format": "int"
		}],
		"label": "活动人数"
	},
	"contact_name": {
		"rules": [{
			"errorMessage": '请选择联系人',
		}, {
			"format": "string"
		}],
		"label": "联系人"
	},
	"contact_phone": {
		"rules": [{
				"required": true,
				"errorMessage": '请选择联系电话',
			}, {
				"format": "string"
			},
			{
				"pattern": "^\\+?[0-9-]{3,20}$"
			}
		],
		"title": "联系电话",
		"label": "联系电话"
	},
	"group_qrcode": {
		"rules": [{
				"format": "array"
			},
			{
				"maxLength": 1
			}
		],
		"label": "群聊二维码"
	},
	"sort": {
		"rules": [{
			"format": "int"
		}],
		"label": "排序"
	},
	"is_recommend": {
		"rules": [{
			"format": "bool"
		}],
		"label": "精选"
	}
}

const enumConverter = {}

function filterToWhere(filter, command) {
	let where = {}
	for (let field in filter) {
		let {
			type,
			value
		} = filter[field]
		switch (type) {
			case "search":
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value)
				}
				break;
			case "select":
				if (value.length) {
					let selectValue = []
					for (let s of value) {
						selectValue.push(command.eq(s))
					}
					where[field] = command.or(selectValue)
				}
				break;
			case "range":
				if (value.length) {
					let gt = value[0]
					let lt = value[1]
					where[field] = command.and([command.gte(gt), command.lte(lt)])
				}
				break;
			case "date":
				if (value.length) {
					let [s, e] = value
					let startDate = new Date(s)
					let endDate = new Date(e)
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
			case "timestamp":
				if (value.length) {
					let [startDate, endDate] = value
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
		}
	}
	return where
}

export {
	validator,
	enumConverter,
	filterToWhere
}